package cn.edu.zzuli.service.impl;

import cn.edu.zzuli.domain.User;
import cn.edu.zzuli.mapper.UserMapper;
import cn.edu.zzuli.service.UserService;
import cn.edu.zzuli.utils.DataGridView;
import cn.edu.zzuli.vo.UserVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by CaoShi on 2020/2/14 10:19
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> listAllUser() {
        return userMapper.findAllUsers();
    }

    @Override
    public DataGridView getUserForDataGridView(UserVO userVO) {
        Page<Object> page = PageHelper.startPage(userVO.getPage(), userVO.getLimit());
        String range = userVO.getBirthdayRange();
        System.out.println("UserServiceImpl.getUserForDataGridView" + range);
        if (range != null && range.length() != 0) {
            String[] strs = range.split("-");
            String startYearStr = strs[0].trim();
            String endYearStr = strs[1].trim();
            userVO.setStartYear(Integer.parseInt(startYearStr));
            userVO.setEndYear(Integer.parseInt(endYearStr));
        }
        List<User> data = userMapper.findUser(userVO);
        // System.out.println("UserServiceImpl.getUserForDataGridView  " + page.getTotal());
        // page.getTotal() 获取数据总条数
        return new DataGridView(page.getTotal(), data);
    }

    @Override
    public int removeUserById(Integer id) {
        return userMapper.deleteUserById(id);
    }

    @Override
    public int saveUser(User user) {
        return userMapper.addUser(user);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public int batchRemoveUser(Integer[] ids) {
        return userMapper.batchDeleteByIds(ids);
    }

    @Override
    public List<String> listAllAddress() {
        return userMapper.findAllAddress();
    }

}
